<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext" v-if="$formOperates.read('flowTitle',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.flowTitle" name="flowTitle" :disabled="judge('flowTitle')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('billNo',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.billNo" name="billNo" :disabled="judge('billNo')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent" v-if="$formOperates.read('flowUrgent',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.flowUrgent" name="flowUrgent" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyUser',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>申请人员</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.applyUser" name="applyUser" :disabled="judge('applyUser')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyDate',formOperatesData)">
							<view class="formgroup">
								<view class="labletitle"><label>申请日期</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.applyDate" name="applyDate" :disabled="judge('applyDate')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" v-if="$formOperates.read('contractName',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>合同名称</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.contractName" name="contractName" placeholder="输入合同名称"
									 :disabled="judge('contractName')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('contractNum',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>合同号</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.contractNum" name="contractNum" placeholder="输入合同号" :disabled="judge('contractNum')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('contractType',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>合同类型</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.contractType" name="contractType" placeholder="输入类型" :disabled="judge('contractType')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('contractId',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>编号支出</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.contractId" name="contractId" placeholder="输入编号" :disabled="judge('contractId')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('firstParty',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>签署方(甲方)</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.firstParty" name="firstParty" placeholder="输入甲方" :disabled="judge('firstParty')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('secondParty',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>乙方</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.secondParty" name="secondParty" placeholder="输入乙方" :disabled="judge('secondParty')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('personCharge',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>合作负责人</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.personCharge" name="personCharge" placeholder="输入负责人" :disabled="judge('personCharge')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('leadDepartment',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>所属部门</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.leadDepartment" name="leadDepartment" placeholder="输入部门"
									 :disabled="judge('leadDepartment')" />
								</view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('signArea',formOperatesData)">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>签订地区</label><text class="required">*</text></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="contractSheet.signArea" name="signArea" maxlength="25" :disabled="judge('signArea')" placeholder="输入" />
									</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" v-if="$formOperates.read('incomeAmount',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>收入金额</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="digit" v-model="contractSheet.incomeAmount" name="incomeAmount" placeholder="输入金额" :disabled="judge('incomeAmount')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('totalExpenditure',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>支出总额</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="digit" v-model="contractSheet.totalExpenditure" name="totalExpenditure" placeholder="输入总额" :disabled="judge('totalExpenditure')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('contractPeriod',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>合同期限</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.contractPeriod" name="contractPeriod" placeholder="输入合同期限" :disabled="judge('contractPeriod')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="paymentMethod" v-if="$formOperates.read('paymentMethod',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>结算方式</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.paymentMethod" name="paymentMethod" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('budgetaryApproval',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>预算批付</label></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.budgetaryApproval" name="budgetaryApproval" placeholder="输入预算批付" :disabled="judge('budgetaryApproval')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="startDate" v-if="$formOperates.read('startContractDate',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开始时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.startContractDate" name="startContractDate" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="endDate" v-if="$formOperates.read('endContractDate',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>结束时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="contractSheet.endContractDate" name="endContractDate" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('contractContent',formOperatesData)">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>内容简要</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="contractSheet.contractContent" name="contractContent" maxlength="100" :disabled="judge('contractContent')" placeholder="输入"/>
								</view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('fileJson',formOperatesData)">
							<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
						</view>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList"  ref="picker"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	
	import config from '@/common/js/config.js'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"paymentMethod", checkType : "notnull", checkRule:"",  errorMsg:"选择结算方式"},
			{name:"startContractDate", checkType : "notnull", checkRule:"",  errorMsg:"选择开始时间"},
			{name:"endContractDate", checkType : "notnull", checkRule:"",  errorMsg:"选择结束时间"},
			{name:"contractName", checkType : "notnull", checkRule:"",  errorMsg:"输入合同名称"},
			{name:"contractNum", checkType : "notnull", checkRule:"",  errorMsg:"输入合同号"},
			{name:"firstParty", checkType : "notnull", checkRule:"",  errorMsg:"输入签署方"},
			{name:"secondParty", checkType : "notnull", checkRule:"",  errorMsg:"输入乙方"},
			{name:"incomeAmount", checkType : "money", checkRule:"",  errorMsg:"输入合格金额"},
			{name:"totalExpenditure", checkType : "money", checkRule:"",  errorMsg:"输入合格总额"},
			{name:"signArea", checkType : "string", checkRule:"1,25",  errorMsg:"地址应为1-25个字符"},
		];
	export default {
		components: {
			wPicker,
			flowbefore,
			
		},
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				contractSheet: {
					billNo: '',
					flowUrgent: '',
					flowTitle: jnpf.userInfo().realName+'的合同申请表',
					applyUser:jnpf.userInfo().realName+"/"+jnpf.userInfo().account,
					applyDate:helper.toDate(new Date, 'yyyy-MM-dd'),
					contractId:'',
					contractNum:'',
					firstParty:'',
					secondParty:'',
					contractName:'',
					contractType:'',
					personCharge:'',
					leadDepartment:'',
					signArea:'',
					incomeAmount:'',
					totalExpenditure:'',
					contractPeriod:'',
					budgetaryApproval:'',
					startContractDate: '',
					endContractDate: '',
					contractContent:'',
					paymentMethod:'',
					fileJson:'',
					
				},
				fileList:[],
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				paymentMethodList:jnpf.dictionaryData('WFSettlementMethod'),
				statusType:'',
				formOperatesData:[],
				F_Status: 0
			}
		},
		// created() {
		// 	this.$eventHub.$off('formOperates');
		// 	this.$eventHub.$on('formOperates', (data) => {
		// 		console.log(data)
		// 		this.formOperatesData = data
		// 		uni.setStorageSync('formOperatesData',data)
		// 	})
		// },
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			let F_Status = uni.getStorageSync('F_Status')
			if(F_Status == 1 || F_Status == 2){
				this.disabled = true;
			}
			/* 工作区进入状态 End*/
			
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.contractSheet.flowId=flowInfo.id;
				this.status = 0;
				uni.setStorageSync('F_Status', 0);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.contractSheet.flowUrgent=helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/system/Base/BillRule/BillNumber/WF_ContractApprovalSheetNo',
					success: res => {
						this.contractSheet.billNo=res.data.data;
					}
				});
				this.getOperatesData(flowInfo.id)
			}
			if(flowInfo.operstate=='info'){
				uni.setStorageSync('F_Status', F_Status);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/workflow/Form/ContractApprovalSheet/' + flowInfo.id,
					success: res => {
						let _contractSheet=res.data.data;
						this.contractSheet = _contractSheet;
						this.contractSheet.flowUrgent=helper.getSpinnerNew(this.urgentList,_contractSheet.flowUrgent);
						this.contractSheet.paymentMethod=helper.getSpinnerNew(this.paymentMethodList,_contractSheet.paymentMethod);
						this.contractSheet.applyDate=helper.toDate(_contractSheet.applyDate, "yyyy-MM-dd");
						this.contractSheet.startContractDate=helper.toDate(_contractSheet.startContractDate, "yyyy-MM-dd HH:mm");
						this.contractSheet.endContractDate=helper.toDate(_contractSheet.endContractDate, "yyyy-MM-dd HH:mm");
						if(_contractSheet.fileJson!=""){
							this.fileList=JSON.parse(_contractSheet.fileJson);
						}
					}
				});
				if(this.status == 0 || this.status == 3){
					this.getOperatesData(flowInfo.flowId)
				}else{
					this.$eventHub.$off('formOperates');
					this.$eventHub.$on('formOperates', (data) => {
						this.formOperatesData = data;
						console.log(this.formOperatesData)
						uni.setStorageSync('formOperatesData', data)
					})
				}
			}
		},
		methods: {
			getOperatesData(id){
				helper.ajax({
					url: '/api/workflow/Engine/FlowEngine/'+id,
					success: res => {
						let kk = JSON.parse(res.data.data.flowTemplateJson);
						this.formOperatesData = [];
						this.formOperatesData = kk.properties.formOperates
						uni.setStorageSync('formOperatesData',this.formOperatesData)
					}
				});
			},
			//判断表单权限可写
			judge(id) {
				return this.statusType == 'backlog' ? this.$formOperates.write(id, this.formOperatesData) : this.F_Status ==
					1 || this.F_Status == 2 ? true : this.$formOperates.write(id, this.formOperatesData)
			},
			flowUrgent(e) {
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('flowUrgent')
				if(!isWrite){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.contractSheet.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			startDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('startDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.contractSheet.startContractDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			endDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('endDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.contractSheet.endContractDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			paymentMethod(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('paymentMethod')
				this.pickerList= this.paymentMethodList;
				if(!isWrite){
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.contractSheet.paymentMethod = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			submit(e){
				this.contractSheet.fileJson = JSON.stringify(this.fileList)
				let formData=this.contractSheet;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.paymentMethod=helper.setSpinnerNew(this.paymentMethodList,formData.paymentMethod);
				formData.status = 0;
				let method = "POST";
				let url = "/api/workflow/Form/ContractApprovalSheet";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.startContractDate = Date.parse(formData.startContractDate);
				formData.endContractDate = Date.parse(formData.endContractDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				this.contractSheet.fileJson = JSON.stringify(this.fileList)
				let formData=this.contractSheet;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.paymentMethod=helper.setSpinnerNew(this.paymentMethodList,formData.paymentMethod);
				formData.status = 1;
				let method = "POST";
				let url = "/api/workflow/Form/ContractApprovalSheet";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.startContractDate = Date.parse(formData.startContractDate);
				formData.endContractDate = Date.parse(formData.endContractDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
		
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
